package com.etennis.service.impl;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.etennis.pojos.PageResult;
import com.etennis.mapper.MemberMapper;
import com.etennis.mapper.SeasonInfoMapper;
import com.etennis.pojos.dto.web.MemberDTO;
import com.etennis.pojos.dto.web.MemberPageQueryDTO;
import com.etennis.pojos.vo.web.GroupMVPVO;
import com.etennis.pojos.vo.web.MVPVO;
import com.etennis.pojos.vo.web.MemberVO;
import com.etennis.pojos.entity.Member;
import com.etennis.pojos.entity.Seasoninfo;
import com.etennis.service.IMemberService;
import com.etennis.utils.BeanCopyUtils;
import com.etennis.utils.PageUtil;
import com.etennis.utils.RandomUtil;
import com.etennis.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static com.etennis.constants.IconConstant.USER_ICON;
import static com.etennis.constants.MemberConstant.DEFAULT_PASSWORD;

@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements IMemberService {
    @Autowired
    MemberMapper memberMapper;
    @Autowired
    SeasonInfoMapper seasonInfoMapper;
    @Autowired
    PasswordEncoder passwordEncoder;
    @Autowired
    RedisCache redisCache;

    /**
     *
     * @param memberDTO 会员名、密码、组别
     */
    @Override
    public void saveMember(MemberDTO memberDTO) {
        Member member = BeanCopyUtils.copyBean(memberDTO, Member.class);
        member.setPassword(passwordEncoder.encode(DEFAULT_PASSWORD));
        String url=USER_ICON;
        member.setIcon(url);
        String account= RandomUtil.generateAccount();
        member.setAccount(account);
        memberMapper.insert(member);
        Seasoninfo seasoninfo=new Seasoninfo();
        seasoninfo.setMemberId(member.getId());

        Long seasonId= redisCache.getCurSeason();
        seasoninfo.setSeasonId(seasonId);
        seasoninfo.setGroupId(memberDTO.getGroupId());
        seasonInfoMapper.insert(seasoninfo);
    }

    @Override
    public void updateMember(MemberDTO memberDTO) {
        if(!Objects.isNull(memberDTO.getPassword())){
            memberDTO.setPassword(passwordEncoder.encode(memberDTO.getPassword()));
        }
        Member member = BeanCopyUtils.copyBean(memberDTO, Member.class);
        memberMapper.updateById(member);
        Seasoninfo seasoninfo=new Seasoninfo();
        // 只能更改本season的组别
        seasoninfo.setGroupId(memberDTO.getGroupId());

        Long seasonId= redisCache.getCurSeason();
        seasonInfoMapper.update(seasoninfo,new LambdaUpdateWrapper<Seasoninfo>().eq(Seasoninfo::getMemberId, memberDTO.getId()).eq(Seasoninfo::getSeasonId,seasonId));
    }

    /**
     *
     * @param id 会员id
     */
    @Override
    public void deleteMember(Long id) {
        // member中isExitClub 改为1
        Member member = new Member();
        member.setId(id);
        member.setIsExitClub("1");
        memberMapper.updateById(member);

        seasonInfoMapper.delete(new LambdaUpdateWrapper<Seasoninfo>().eq(Seasoninfo::getMemberId,id));
    }

    @Override
    public PageResult<MemberVO> pageMember(MemberPageQueryDTO memberPageQueryDTO) {

        Page<Member> page = PageUtil.convert(memberPageQueryDTO);
        Page<MemberVO> memberVOPage = memberMapper.pageQuery(page, memberPageQueryDTO);
        PageResult res = PageUtil.convert(memberVOPage);
        return res;
    }

    @Override
    public GroupMVPVO groupMVP() {
        List<MVPVO> groupMVP=new ArrayList<>();
        // todo seasonId
        //Long seasonId = redisCache.getCurSeason();
        Long seasonId=1L;
        for (Long i = 1L; i <= 4L; i++) {
            MVPVO mvpvo=memberMapper.groupMVP(i,seasonId);
            groupMVP.add(mvpvo);
        }
        return new GroupMVPVO(groupMVP);
    }
}
